/**
 * 简洁特效幻灯类
 * 
 * @author XingDongHai (http://www.xingdonghai.cn)
 * @version 0.20 (2012-06-13)
 * 
 * 使用方法：
 * 在页面的onload事件中加入 xSlider.init(); 参数json格式
 * 
 * @param int width : 显示区域宽度
 * @param string wrap : 幻灯外层元素DOM ID (必须)
 * @param string imgwrap : 幻灯图片列表的外层元素DOM ID, 未指定时会自动将wrap的第一个子节点作为其值
 * @param string numwrap : 幻灯按钮列表的外层元素DOM ID, 未指定时会自动将wrap的第二个子节点作为其值; 在不指定并且该节点不存在时则不进行处理
 * @param int pause : 每次切换所需等待的毫秒数, 默认为5000
 * @param int time : 滑动延迟, 默认为35, 单位为 毫秒
 * @param int step : 滑动步进, 默认为7
 * @param function onstart : 滑动开始时触发的函数, 自动将当前索引值和对象作为参数传入, 起始为0, 其中第一个参数为索引值, 第二个参数为DOM对象
 * @param function onfinish : 滑动结束时触发的函数, 自动将当前索引值和对象作为参数传入, 起始为0, 其中第一个参数为索引值, 第二个参数为DOM对象
 */

var xSlider = {
	D : {
		width : null,
		wrap : null,
		imgwrap : null,
		numwrap : null,
		index : 0,
		count : 0,
		target : 0,
		pause : 5000,
		time : 35,
		step : 7,
		auto : true,
		rolltimer : null,
		onstart : function(){},
		onfinish : function(){}
	},
	
	$ : function(id) {
		return 'string' == typeof(id) ? document.getElementById(id) : id;
	},
	
	extend : function(target, source) {
		for (var i in source) {
			target[i] = source[i];
		}
		return target;
	},

	init : function(options) {
		this.D = this.extend(this.D, options);
		if (!this.D.wrap || !this.D.width)
			return this.halt('xSlider Message : Lack of required parameters');
		this.D.wrap = this.$(this.D.wrap);
		this.D.width = parseInt(this.D.width);

		if (!this.D.imgwrap) {
			var t = this.getChildNodes(this.D.wrap);
			if (!t.length)
				return this.halt('xSlider Message : Parameter Error');
			this.D.imgwrap = t[0];
			this.D.numwrap = t[1] || null;
		} else {
			this.D.imgwrap = this.$(this.D.imgwrap);
			if (this.D.numwrap)
				this.D.numwrap = this.$(this.D.numwrap);
		}

		var imgs = this.getChildNodes(this.D.imgwrap);
		this.D.count = imgs.length;
		
		if (this.D.count > 1) {
			if (this.D.numwrap) {
				this.D.numwrap = this.getChildNodes(this.D.numwrap);
				if (this.D.count > this.D.numwrap)
					return this.halt('xSlider Message : Lack of control button');
				this.D.numwrap[0].className = this.D.numwrap[0].className + ' xslider_hover';
				for (var i in this.D.numwrap) {
					this.D.numwrap[i].onmouseover = new function(i){
						return function(){
							xSlider.D.auto = false;
							clearTimeout(xSlider.D.rolltimer);
							xSlider.D.auto = true;
							xSlider.start(i);
						}
					}(i);	
				}
			}

			this.D.imgwrap.style.width = (this.D.width * imgs.length) + 'px';
			this.each(imgs, function(o){
				o.style.width = xSlider.D.width + 'px';
			});
			this.D.rolltimer = setTimeout(function(){xSlider.start();}, this.D.pause);
		}
	},
	
	start : function(i) {
		this.D.numwrap[this.D.index].className = this.D.numwrap[this.D.index].className.replace(' xslider_hover', '');
		if ('undefined' == typeof(i)) {
			this.D.index++;
			if (this.D.index >= this.D.count)
				this.D.index = 0;
		} else {
			this.D.index = i;	
		}

		this.D.numwrap[this.D.index].className = this.D.numwrap[this.D.index].className + ' xslider_hover';
		this.D.target = this.D.index * -this.D.width;
		this.D.onstart(this.D.index, this.D.numwrap[this.D.index]);
		this.roll();
	},
	
	roll : function() {
		var old = parseInt(this.D.imgwrap.style.marginLeft) || 0;
		var step = this.getStep(old);
		if (step) {
			this.D.imgwrap.style.marginLeft = old + step + 'px';
			if (this.D.auto)
				this.D.rolltimer = setTimeout(function(){xSlider.roll();}, this.D.time);
		} else {
			this.D.imgwrap.style.marginLeft = this.D.target + 'px';
			this.D.onfinish(this.D.index, this.D.numwrap[this.D.index]);
			if (this.D.auto)
				this.D.rolltimer = setTimeout(function(){xSlider.start();}, this.D.pause);
		}
	},
	
	getStep : function(old) {
		var offset = (this.D.target - old) / 7;
		if (0 == offset)
			return 0;
		else if (Math.abs(offset) < 1) 
			return offset < 0 ? -1 : 1;
		else
			return offset;
	},
	
	halt : function(s) {
		alert(s);
		return false;	
	},
	
	getChildNodes : function(p) {
		var t = p.childNodes;
		var r = [];
		for (var i in t) {
			if (1 == t[i].nodeType && 'SCRIPT' != t[i].tagName)
				r.push(t[i]);	
		}
		return r;
	},
	
	each : function(objs, func, e) {
		for (var i in objs) {
			if (e)
				objs[i].e = func();
			else
				func(objs[i]);	
		}
	}
};